
import java.awt.Component;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;


public class ImportSetup extends Frame implements ActionListener{
String ip = null;
int port = 0;
String key = null;
	/**
	 * 
	 */
	private static final long serialVersionUID = -7796097434388198089L;
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		JFileChooser choose = new JFileChooser();
		javax.swing.filechooser.FileFilter filter = new javax.swing.filechooser.FileFilter(){

			@Override
			public boolean accept(File f) {
				if(f.isDirectory()) return true;
				if(f.getName().toLowerCase().endsWith(".txt")) return true;
				return false;
			}

			@Override
			public String getDescription() {
				return null;
			}
			
		};
		choose.addChoosableFileFilter(filter);
		int value = choose.showOpenDialog((Component) arg0.getSource());
		if(value == JFileChooser.CANCEL_OPTION)
			choose.setVisible(false);
		if(value == JFileChooser.APPROVE_OPTION){
			File file = choose.getSelectedFile();
			try {
				FileInputStream stream = new FileInputStream(file);
				DataInputStream input = new DataInputStream(stream);
				BufferedReader read = new BufferedReader(new InputStreamReader(input));
				try {
					ip = read.readLine();
					String portTemp = read.readLine();
					port = Integer.parseInt(portTemp);
					key = read.readLine();
					JOptionPane.showMessageDialog(this, "File Uploaded Successfully");
					ClientSideSocket socket = new ClientSideSocket(ip,port,key);
					socket.makeSocket();
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
			
	}
}

